/*********************************************************************************
Description: This is a main function file
Author: SA12226114(Xin Zhu), SA12226139(Dan Su)
Date: 2012-11 

Copyright (c) 2012-2022 School of Software Engineering of USTC, All rights reserved.

**********************************************************************************/



#include "dbclient.h"




/*****************************************************************
Function Name: main
Description:
Parameter:  

@argc: argument count
@argv: argument vector

Author: 
Date:  
******************************************************************/
int main(int argc, char** argv)
{
    int sockfd_router, sockfd_memcache, sockfd_dbserver;
    char recvline[MAXLINE], sendline[CMDLINE_LEN] = {0};
    char memcache_ip[32];
    struct sockaddr_in servaddr;
    char cmd[CMD_LEN], key[KEY_LEN], value[VALUE_LEN], retval[VALUE_LEN] = {0};

    /* check whether argument count is right */
    if(argc != 2)
    {
        printf("usage: ./client <ipaddress>\n");
        
        return 0;
    }
    
    //util_menu();

    
    /* generate a new socket for client */
    sockfd_router = sw_create_socket(PF_INET, SOCK_STREAM, 0);
    
    /* initialize structure sockaddr_in with protocol family, port number, ip */
    sw_init_socket(&servaddr, sizeof(servaddr), argv[1]);
    
    /* connect server using socket id */
    if(!sw_connect(sockfd_router, (struct sockaddr*)&servaddr, sizeof(servaddr)))
        return;
    
    //sw_recv(sockfd, sendline, CMDLINE_LEN, 0);
    //printf("before");
    /* receive command and send it to server. */
    util_init(sendline, CMDLINE_LEN);
    while(1)
    {
        printf(">>>");
        util_getcommandline(sendline, CMDLINE_LEN);

        util_init(cmd, CMD_LEN);        
        util_parse(sendline, CMDLINE_LEN, cmd, key, value);
        util_memset(cmd, CMD_LEN);

        if(strncmp(cmd, "set", sizeof("set")) == 0 ||
           strncmp(cmd, "get", sizeof("get")) == 0 ||
           strncmp(cmd, "remove", sizeof("remove")) == 0 ||
           strncmp(cmd, "ls", sizeof("ls")) == 0 ||
           strncmp(cmd, "delete", sizeof("delete")) == 0)
        {
            /* send command to router and get memcache node's ip which client should access to */
            sw_send(sockfd_router, sendline, strlen(sendline), 0);
            sw_recv(sockfd_router, recvline, MAXLINE, 0);

            strncpy(memcache_ip, recvline, 32);
            printf("%s \n", memcache_ip);

            /* send command to memcache node and get info from memcache */
            sw_connect_server(memcache_ip, &sockfd_memcache);
            sw_send(sockfd_memcache, sendline, strlen(sendline), 0);
            sw_recv(sockfd_memcache, recvline, MAXLINE, 0);
            printf("%s \n", recvline);
            
        }
        else if(strncmp(cmd, "open", sizeof("open")) == 0)
        {
            sw_connect_server("192.168.10.94", &sockfd_dbserver);
            sw_send(sockfd_dbserver, sendline, strlen(sendline), 0);
            sw_recv(sockfd_dbserver, recvline, MAXLINE, 0);
            printf("%s \n", recvline);

        }
        else if(strncmp(cmd, "help", sizeof("help")) == 0)              /* display menu info */
        {
            util_menu();
            continue;
        }
        else if(strncmp(cmd, "quit", sizeof("quit")) == 0)              /* quit system */
        {
            return; 
        }
         else if(strncmp(cmd, "close", sizeof("close")) == 0)              /* quit system */
        {
            
            sw_connect_server("192.168.10.94", &sockfd_dbserver);
            sw_send(sockfd_dbserver, sendline, strlen(sendline), 0);
            sw_recv(sockfd_dbserver, recvline, MAXLINE, 0);
            printf("%s \n", recvline);

            return;
        }
       

        util_init(sendline, CMDLINE_LEN);
    }

    sw_close(sockfd_router);

    return 0;
}





